package com.bonc.modules.common.controller;

import com.bonc.common.utils.Constant;
import com.bonc.common.utils.RedisUtils;
import com.bonc.common.utils.Result;
import com.bonc.common.utils.ShiroUtils;
import com.bonc.modules.common.entity.Region;
import com.bonc.modules.common.service.CommonService;
import com.bonc.modules.sys.entity.SysUserEntity;
import com.bonc.modules.sys.service.OriginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author jinlong
 * @date 2021/6/28
 */
@RestController
@RequestMapping(value = "/common")
public class CommonController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private OriginUserService originUserService;

    /**
     * 地市列表
     */
    @GetMapping("/city/select")
    public Result citySelect(Region region) {

        SysUserEntity userEntity = ShiroUtils.getUserEntity();

        //如果不是超级管理员，则只查询自己所拥有的角色列表
        if (userEntity.getUserId() != Constant.SUPER_ADMIN  ) {
//            map.put("create_user_id", getUserId());
            region.setCityCode(userEntity.getCityCode());
            region.setCountyCode(userEntity.getCountyCode());
        }
        List<Region> list = commonService.getCity(region);

        return Result.ok().put("list", list);
    }

    /**
     * 地市列表
     */
    @GetMapping("/county/select")
    public Result countySelect(Region region) {

        SysUserEntity userEntity = ShiroUtils.getUserEntity();

        //如果不是超级管理员，则只查询自己所拥有的角色列表
        if (userEntity.getUserId() != Constant.SUPER_ADMIN && !"00".equals(userEntity.getCityCode()) ) {
            region.setCityCode(userEntity.getCityCode());
            region.setCountyCode(userEntity.getCountyCode());
        }
        List<Region> list = commonService.getCounty(region);

        return Result.ok().put("list", list);
    }

    /**
     * 地市列表
     */
    @GetMapping("/grid/select")
    public Result gridSelect(Region region) {

        SysUserEntity userEntity = ShiroUtils.getUserEntity();

        //如果不是超级管理员，则只查询自己所拥有的角色列表
        if (userEntity.getUserId() != Constant.SUPER_ADMIN  &&!"00".equals(userEntity.getCityCode()) ) {
//            map.put("create_user_id", getUserId());
            region.setCityCode(userEntity.getCityCode());
            region.setCountyCode(userEntity.getCountyCode());
        }
        List<Region> list = commonService.getGrid(region);

        return Result.ok().put("list", list);
    }


}
